//
//  TSPreInvestView.swift
//  TSProfitProject
//
//  Created by TMS on 2020/4/7.
//  Copyright © 2020 TMS. All rights reserved.
//

import UIKit

protocol TSPreInvestViewDelegate : NSObjectProtocol {
    func preCompleteInvest(_ args : (Array<String>, String, String))
}

class TSPreInvestView: UIView {
    
    weak var delegate : TSPreInvestViewDelegate?
    
    fileprivate var unitLabel : UILabel!
    fileprivate var timeLabel : UILabel!
    fileprivate var resultLabel : UILabel!
    fileprivate var costLabel : UILabel!
    
    fileprivate var args : (Array<String>, String, String)?

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.6)
        configViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupView(args: (Array<String>, String, String)) {
        
        self.args = args
        
        let array = args.0
        
        self.unitLabel.text = getCurrentRank(rank: args.2)
        self.timeLabel.text = args.1
        self.resultLabel.text = array.joined(separator: ",")
        self.costLabel.text = "\(6*Int(args.1)!)"
    }
    
    @objc func cancelAction() {
        self.isHidden = true
    }
    
    @objc func sureAction() {
        self.isHidden = true
        if self.delegate != nil {
            self.delegate?.preCompleteInvest(self.args!)
        }
    }
    
    fileprivate func configViews() {
        
        let container = UIView.init()
        container.backgroundColor = WHITE_COLOR
        container.layer.cornerRadius = 5
        container.layer.masksToBounds = true
        addSubview(container)
        container.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
            make.width.equalTo(SCREEN_WIDTH - 2*15)
            make.height.equalTo(160)
        }
        
        let titleLabel = createLabel(title: "本期投注", fontSize: 16)
        titleLabel.font = UIFont.systemFont(ofSize: 18, weight: .medium)
        container.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.left.equalTo(15)
            make.top.equalTo(20)
        }
        
        let unitLabel = createLabel(title: "名次", fontSize: 14)
        let timeLabel = createLabel(title: "注数", fontSize: 14)
        let numLabel = createLabel(title: "号码", fontSize: 14)
        let costLabel = createLabel(title: "倍数/元", fontSize: 14)
        
        container.addSubview(unitLabel)
        container.addSubview(timeLabel)
        container.addSubview(numLabel)
        container.addSubview(costLabel)
        
        let array = [unitLabel, timeLabel, numLabel, costLabel]
        array.snp.distributeViewsAlong(axisType: .horizontal, fixedSpacing: 0, leadSpacing: 0, tailSpacing: 0)
        array.snp.makeConstraints { (make) in
            make.top.equalTo(titleLabel.snp.bottom).offset(10)
            make.height.equalTo(20);
        }
        
        self.unitLabel = createLabel(title: "/", fontSize: 14)
        self.timeLabel = createLabel(title: "/", fontSize: 14)
        self.resultLabel = createLabel(title: "/", fontSize: 14)
        self.costLabel = createLabel(title: "/", fontSize: 14)
        
        container.addSubview(self.unitLabel)
        container.addSubview(self.timeLabel)
        container.addSubview(self.resultLabel)
        container.addSubview(self.costLabel)
        
        let valueArray = [self.unitLabel, self.timeLabel, self.resultLabel, self.costLabel]
        valueArray.snp.distributeViewsAlong(axisType: .horizontal, fixedSpacing: 0, leadSpacing: 0, tailSpacing: 0)
        valueArray.snp.makeConstraints { (make) in
            make.top.equalTo(unitLabel.snp.bottom).offset(7)
            make.height.equalTo(unitLabel)
        }
        
        let cancelBtn = UIButton.init(type: .custom)
        cancelBtn.setTitle("取消", for: .normal)
        cancelBtn.setTitleColor(UIColor.hexadecimalColor(hexadecimal: "#282828"), for: .normal)
        cancelBtn.backgroundColor = UIColor.hexadecimalColor(hexadecimal: "#F6F6F6")
        cancelBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        cancelBtn.addTarget(self, action: #selector(cancelAction), for: .touchUpInside)
        container.addSubview(cancelBtn)
        
        let sureBtn = UIButton.init(type: .custom)
        sureBtn.setTitle("确认", for: .normal)
        sureBtn.backgroundColor = BASE_RED_COLOR
        sureBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        sureBtn.addTarget(self, action: #selector(sureAction), for: .touchUpInside)
        container.addSubview(sureBtn)
        
        let handleArray = [cancelBtn, sureBtn]
        handleArray.snp.distributeViewsAlong(axisType: .horizontal, fixedItemLength: 65, leadSpacing: 45, tailSpacing: 45)
        
        handleArray.snp.makeConstraints { (make) in
            make.top.equalTo(self.unitLabel.snp.bottom).offset(20)
            make.height.equalTo(30)
        }
        
    }
    
    fileprivate func createLabel(title:String, fontSize:CGFloat) -> UILabel {
        let label = UILabel.init()
        label.text = title
        label.textAlignment = .center
        label.font = UIFont.systemFont(ofSize: fontSize)
        label.textColor = UIColor.hexadecimalColor(hexadecimal: "#282828")
        return label
    }
    
    func getCurrentRank(rank:String) -> String {
        
        switch rank {
        case "one":
            return "冠军"
        
        case "two":
            return "亚军"
        
        case "three":
            return "季军"
        
        case "four四名":
            return "四名"
        
        case "five":
            return "五名"
        
        case "six":
            return "六名"
        
        case "seven":
            return "七名"

        case "eight":
            return "八名"
        
        case "nine":
            return "九名"
        
        case "ten":
            return "十名"
        default:
            return "one"
        }
    }
}
